package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpAllotApply;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpAllotApplyLine;
import com.mxpio.erp.common.equipment.service.EquipmentEqpAllotApplyLineService;
import com.mxpio.erp.common.equipment.service.EquipmentEqpAllotApplyService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : wupp
 * @Datetime : 2022/9/15 10:16
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Tag(name = "EquipmentEqpAllotApplyController", description = "设备台账调拨申请接口")
@RestController
@RequestMapping("/erp/equipment/allot/")
public class EquipmentEqpAllotApplyController {
	@Autowired
	private EquipmentEqpAllotApplyService service;
	@Autowired
	private EquipmentEqpAllotApplyLineService lineService;

	@GetMapping("list")
    @Operation(summary = "设备台账调拨列表", description = "获取设备台账调拨列表（无分页）", method = "GET")
    public Result<List<EquipmentEqpAllotApply>> list(@Parameter(name="查询参数")  Criteria criteria)  {
        List<EquipmentEqpAllotApply> applyList = service.list(EquipmentEqpAllotApply.class, criteria);
        return Result.OK(applyList);
    }
	
    @GetMapping("page")
    @Operation(summary = "设备台账调拨列表", description = "获取设备台账调拨列表（分页）", method = "GET")
    public Result<Page<EquipmentEqpAllotApply>> page(@Parameter(name="查询参数")Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo)  {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentEqpAllotApply> equipmentEqpAllotApplys = service.listPage(EquipmentEqpAllotApply.class, page, criteria);
        return Result.OK(equipmentEqpAllotApplys);
    }

    @GetMapping("pageForAudit")
    @Operation(summary = "设备台账调拨审核列表", description = "获取设备台账调拨审核列表（分页）", method = "GET")
    public Result<Page<EquipmentEqpAllotApply>> pageForAudit(@Parameter(name="查询参数")Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo)  {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentEqpAllotApply> equipmentEqpAllotApplys = service.listPage(EquipmentEqpAllotApply.class, page, criteria);
        return Result.OK(equipmentEqpAllotApplys);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取申请单", description = "根据bizNo获取申请单", method = "GET")
    public Result<EquipmentEqpAllotApply> getById(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
    	EquipmentEqpAllotApply equipmentEqpAllotApply = service.getById(EquipmentEqpAllotApply.class, bizNo);
        return Result.OK(equipmentEqpAllotApply);
    }
    
    @RequestMapping("save")
    @Operation(summary = "保存申请单", description = "保存申请单")
    public Result<EquipmentEqpAllotApply> save(@RequestBody EquipmentEqpAllotApply equipmentEqpAllotApply) {
    	service.save(equipmentEqpAllotApply,new SmartCrudPolicyAdapter(){
        	@Override
        	public boolean beforeInsert(CrudContext context) {
        		Object o = context.getEntity();
        		if(o instanceof EquipmentEqpAllotApplyLine){
                    EquipmentEqpAllotApply equipmentEqpAllotApply = context.getParent();
                    ((EquipmentEqpAllotApplyLine) o).setBizNo(equipmentEqpAllotApply.getBizNo());
        		}
        		return true;
        	}
        });
        return Result.OK(equipmentEqpAllotApply);
    }
    
    @DeleteMapping("remove/{bizNos}")
    @Operation(summary = "根据bizNos删除申请单", description = "删除申请单", method = "DELETE")
    @Transactional(readOnly = false)
    public Result<EquipmentEqpAllotApply> remove(@Parameter(name="申请单号，多个单号用英文逗号分隔") @PathVariable(name = "bizNos", required = true) String bizNos) {
        String[] bizNo = bizNos.split(",");
        for(String key : bizNo){
        	Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, key);
        	lineService.removeBatch(EquipmentEqpAllotApplyLine.class, c);
        	service.delete(EquipmentEqpAllotApply.class, key);
        }
        return Result.OK();
    }
    
	@PutMapping("open/{bizNo}")
    @Operation(summary = "打开申请单", description = "打开申请单", method = "PUT")
    public Result<EquipmentEqpAllotApply> open(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
    	return service.updateCloseStatus(bizNo, OrderStatusEnums.CloseStatus.OPEN);
    }

    @PutMapping("close/{bizNo}")
    @Operation(summary = "关闭申请单", description = "关闭申请单", method = "PUT")
    public Result<EquipmentEqpAllotApply> close(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
    	return service.updateCloseStatus(bizNo, OrderStatusEnums.CloseStatus.CLOSED);
    }
    
    @PutMapping("submit/{bizNo}")
    @Operation(summary = "提交申请单", description = "提交申请单", method = "PUT")
    public Result<EquipmentEqpAllotApply> submit(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
    	return service.updateBpmnStatus(bizNo, OrderStatusEnums.BpmnStatus.AUDITING);
    }
    
    @PutMapping("audit/{bizNo}")
    @Operation(summary = "审核申请单", description = "审核申请单", method = "PUT")
    public Result<EquipmentEqpAllotApply> audit(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
    	return service.updateBpmnStatus(bizNo, OrderStatusEnums.BpmnStatus.AUDITED);
    }
    
    @PutMapping("abandon/{bizNo}")
    @Operation(summary = "弃审申请单", description = "弃审申请单", method = "PUT")
    public Result<EquipmentEqpAllotApply> abandon(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
    	return service.updateBpmnStatus(bizNo, OrderStatusEnums.BpmnStatus.CREATE);
    }

    @RequestMapping("excute/{bizNo}")
    @Operation(summary = "执行申请单", description = "执行申请单")
    public Result<EquipmentEqpAllotApply> excute(@PathVariable(name = "bizNo", required = true) String bizNo) {
        Result<EquipmentEqpAllotApply> result = null;
        try {
            result = service.execute(bizNo);
        } catch (MBootException e) {
            return Result.error(e.getMessage());
        }
        return result;
    }

}
